#include <iostream>
#include <cstring>
#include <unistd.h>
#include <stdlib.h>
#include <string>
#include "../../../../mysqlDBConnPool.h"
#include "../../../../picojson.h"
#include "../../../../common.h"
#include "../../../../logger.h"

struct respData
{
  respData()
  {
    success_ = false;
  }
  bool success_;
  std::string message_;
};


int main()
{
  //std::cerr << "*******" << std::endl;

  std::string path = "../../../../../log/server.log";
  COM::logger = new qLogger(path);
  char* m = getenv("METHOD_ENV");
  std::cerr << "******" << m << std::endl;

  if(strcasecmp(m, "POST") == 0)
  {
    respData resp;

    char buf[1024] = { 0 };
    std::string param;
    ssize_t s = read(0, buf, 1024);
    if(s > 0)
    {
      db_col info[] = {
        {"ACCOUNT", DB_STR, sizeof(char)*6},
        {"PASSWD", DB_STR, sizeof(char)*6},
        {nullptr, 0, 0},
      };

      struct UserInfo
      {
        UserInfo()
        {
          bzero(this, sizeof(*this));
        }
        char account[6];
        char passwd[6];
      };
      UserInfo* u = new UserInfo;

      MysqlConnPool* mq = nullptr;
      Connid connid = 0;
      std::string username_param;
      std::string passwd_param;
      char where[256] = { 0 };

      buf[s] = '\0';
      std::string param = buf;
      std::cerr << "main main post " << param[0] << "-" << param[1] << "-" << param.size()  << std::endl;
      //write(1, param.c_str(), s);
      //处理参数，查数据库
      size_t pos = param.find(',');
      if(pos == std::string::npos)
      {
        //TODO
        //返回什么
        resp.success_ = false;
        std::cerr << "," << std::endl;
        goto END;
      }

      username_param = param.substr(0, pos);
      passwd_param = param.substr(pos + 1);

      pos = username_param.find(':');
      if(pos == std::string::npos)
      {
        resp.success_ = false;
        std::cerr << "1:" << std::endl;
        goto END;
      }

      username_param = username_param.substr(pos + 2);
      username_param.pop_back();

      pos = passwd_param.find(':');
      if(pos == std::string::npos)
      {
        resp.success_ = false;
        std::cerr << "2:" << std::endl;
        goto END;
      }

      passwd_param = passwd_param.substr(pos + 2);
      passwd_param.pop_back();
      passwd_param.pop_back();
      std::cerr << "username:" << username_param << " passwd:" << passwd_param << std::endl;

      mq = MysqlConnPool::getInstance();
      mq->init("mysql://root:Root_123@127.0.0.1:3306/testdb");
      connid = mq->getHandle(nullptr);
      sprintf(where, "`ACCOUNT`='%s'", username_param.c_str());
      if(mq->exeSelect(connid, "ACCOUNTTABLE", info, where, nullptr, nullptr, (unsigned char**)&u) == -1)
      {
        resp.message_ = "not select";
        resp.success_ = false;
        std::cerr << "select fail" << where << " : " << std::endl;
        goto END;
      }

      if(strcmp(u->passwd, passwd_param.c_str()) != 0)
      {
        resp.success_ = false;
        std::cerr << u->account  << " cmp fail"  << u->passwd << ":" << passwd_param.c_str() << std::endl;
        goto END;
      }
      resp.success_ = true;
      resp.message_ = "login success";

END:
      picojson::value jsonValue = structToJson(resp);
      std::string jsonStr = jsonValue.serialize();
      std::cerr << "jsonStr:" << jsonStr.c_str() << std::endl;
      write(1, jsonStr.c_str(), jsonStr.size());

    }

    //std::cout << p << std::endl;
  }
  else if(strcasecmp(m, "GET") == 0)
  {
    char* param = getenv("PARAM_ENV");
    //std::cerr << "******" << param << std::endl;
    if(param)
    {
      //std::cerr << "main main get " << param << "main main " << std::endl;
      write(1, param, strlen(param));
    }
  }

  return 0;
}

